#include "interface.h"

int penPosition = PEN_DOWN;

void initInterface() {

	DDRB |= _BV(GREEN) | _BV(RED);	// LED red and green are here
}

void penUp() {

	if (penPosition == PEN_DOWN) {
		changeStatus(STATUS_WARN);
		while((PIND & _BV(LEFTSW)) != 0) {
			_delay_us(10);
		}
		changeStatus(STATUS_OK);
		penPosition = PEN_UP;
	}
}

void penDown() {

	if (penPosition == PEN_UP) {
		changeStatus(STATUS_WARN);
		while((PIND & _BV(LEFTSW)) != 0) {
			_delay_us(10);
		}
		changeStatus(STATUS_OK);
		penPosition = PEN_DOWN;
	}
}

void changeStatus(int status) {

	if (status == STATUS_OK) {
		PORTB |= (1 << GREEN);		// Green on
		PORTB &= ~(1 << RED);		// Red off
	}
	else if (status == STATUS_WARN) {
		PORTB &= ~(1 << GREEN);		// Green off
		PORTB |= (1 << RED);		// Red on
	}
	else {
		PORTB &= ~(1 << GREEN);		// Green off
		PORTB &= ~(1 << RED);		// Red off
	}
}
